//
//  Split.m
//  Bespoke
//
//  Created by Cheng on 12/9/26.
//
//

#import "Split.h"

@implementation NSString (Split)

- (NSString *)StringSplit:(NSString *)Pattern {
    NSString *String = self;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:Pattern options:NSRegularExpressionCaseInsensitive error:nil];
    NSArray *matches = [regex matchesInString:String options:0 range:NSMakeRange(0, String.length)];
    NSTextCheckingResult *match = [matches objectAtIndex:0];
    NSRange range = [match rangeAtIndex:0];
    NSArray *Array_Split = [[String substringWithRange:range] componentsSeparatedByString:@"="];
    String = [Array_Split objectAtIndex:1];
    return String;
}

- (NSString *)StringParseSymbol {
    NSString *String = self;
    String = [String stringByReplacingOccurrencesOfString:@"&amp;" withString:@"&"];
    String = [String stringByReplacingOccurrencesOfString:@"&lt;" withString:@"<"];
    String = [String stringByReplacingOccurrencesOfString:@"&gt;" withString:@">"];
    String = [String stringByReplacingOccurrencesOfString:@"&apos;" withString:@"'"];
    String = [String stringByReplacingOccurrencesOfString:@"&quot;" withString:@"\""];
    return String;
}

- (NSString *)SymbolParseString {
    NSString *String = self;
    String = [String stringByReplacingOccurrencesOfString:@"&" withString:@"&amp;"];
    String = [String stringByReplacingOccurrencesOfString:@"<" withString:@"&lt;"];
    String = [String stringByReplacingOccurrencesOfString:@">" withString:@"&gt;"];
    String = [String stringByReplacingOccurrencesOfString:@"'" withString:@"&apos;"];
    String = [String stringByReplacingOccurrencesOfString:@"\"" withString:@"&quot;"];
    return String;
}


#pragma mark - Rmove URL Function.

- (NSString *)TitleSplitPressed {
    
    NSString *OriginalText = self;
    
    if ([OriginalText rangeOfString:@"http://"].location==NSNotFound)
        return OriginalText;
    else if ([OriginalText rangeOfString:@" "].location==NSNotFound)
        return OriginalText;
    
    NSString *result = [[NSString alloc] init];
    NSRange range;
    
    NSArray *Array_Titlesplit = [OriginalText componentsSeparatedByString:@" "];
    for (NSString *Titlesplit in Array_Titlesplit) {
        range = [Titlesplit rangeOfString:@"http://"];
        if (range.location == NSNotFound)
        {
            result = [result stringByAppendingString:Titlesplit];
            result = [result stringByAppendingFormat:@" "];
        }
        
        //Text contain "http", but no " ".
        if (Array_Titlesplit.count==1)
            result = [Titlesplit substringToIndex:range.location];
    }
    
    return result;
}

@end
